#!/bin/sh
# postinst script for openmediavault-mysql
#
# $Id: postinst 29 2011-11-15 13:32:30Z imooreyahoo@gmail.com $
#
# see: dh_installdeb(1)

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

case "$1" in
    configure)

	# Fix permissions
	omv_install_fixperms

	FIRSTINSTALL=""
	STARTPOSTFIX=""
	
	# If primary e-mail isn't root, assume we haven't been installed before
	if [ "$(omv_config_get "//system/email/primaryemail")" != "root@localhost" ]; then
		FIRSTINSTALL="1"
	fi
	
	# If postfix wasn't enabled, it probably needs to be started
	if [ "$(omv_config_get "//system/email/enable")" != "1" ]; then
		STARTPOSTFIX="1"
	fi

	# Update postfix config
	omv-mkconf zpostfix
	
	# Enable init.d script
	update-rc.d postfix defaults

	# First install updates
	if [ "${FIRSTINSTALL}" != "" ]; then
	
		# Update postfix config
		omv-mkconf postfix 2>&1 >/dev/null
		omv-mkconf zpostfix
		
		# Restart postfix
		/etc/init.d/postfix restart
		
		# Update anything using "primaryemail"
		for I in $(grep -l primaryemail /usr/share/openmediavault/scripts/* | grep -ve "/postfix$")
		do
			omv-mkconf $(basename $I)
		done

	elif [ "${STARTPOSTFIX}" != "" ]; then
	
		/etc/init.d/postfix start
	
	else
	
		omv-mkconf zpostfix
		
	fi
	
	chown root:root /usr/local/bin/omvmbox.py
	chmod 0700 /usr/local/bin/omvmbox.py
	
    ;;

	# Notifications need to be udpated after an OMV upgrade
    triggered)

		# Update postfix config
		omv-mkconf postfix 2>&1 >/dev/null
		omv-mkconf zpostfix
		
		# Restart postfix
		/etc/init.d/postfix restart
		
		# Update anything using "primaryemail"
		for I in $(grep -l primaryemail /usr/share/openmediavault/scripts/* | grep -ve "/postfix$")
		do
			omv-mkconf $(basename $I)
		done
		
	;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
